package controller;

import domain.FamilyBookItem;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
import tools.JDBCUtils;
import view.Detile;

import javax.swing.*;
import java.awt.*;
import java.sql.*;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Objects;

/**
 * @author :wn
 * @program : 管家婆项目
 * @descript : 编辑
 * @create :2020-06-17 18:59
 */

public class EditFrame {
    @FXML
    private TextField Editid;
    @FXML
    private TextField Edittype;
    @FXML
    private TextField Editmoney;
    @FXML
    private TextField Editdate;
    @FXML
    private TextField Editdescription;

    public void edit() throws SQLException, ClassNotFoundException, ParseException {

        Statement smt = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost:3306/familybook?useSSL=false";
        Connection conn = JDBCUtils.getConnection(url);
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date editdate = dateFormat.parse(Editdate.getText());
        java.sql.Date editdate1 = new java.sql.Date(editdate.getTime());
        int editid = Integer.parseInt(Editid.getText());
        String edittype = Edittype.getText();
        float editmoney = Float.parseFloat(Editmoney.getText());
        String editdescriptions = Editdescription.getText();
        FamilyBookItem familyBookItem = new FamilyBookItem(editid, edittype, editmoney, editdate1, editdescriptions);
        smt = conn.createStatement();
        String sql = "update bill set taketype='" + familyBookItem.getType() + "',money=" + familyBookItem.getMoney() + ",days='" + editdate1 + "',descriptions='" + familyBookItem.getDescription() + "' where  id=" + familyBookItem.getNumber() + "";
        if(smt.execute(sql)==true) {
            JOptionPane.showMessageDialog(null,"修改失败");
        }else{
            JOptionPane.showMessageDialog(null, "修改成功");
        }
    }

        public void additem() throws SQLException, ClassNotFoundException, ParseException {
        Statement smt=null;
        ResultSet rs=null;
        String url="jdbc:mysql://localhost:3306/familybook?useSSL=false";
        Connection conn=JDBCUtils.getConnection(url);
            DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
            Date editdate=dateFormat.parse(Editdate.getText());
            java.sql.Date editdate1=new java.sql.Date(editdate.getTime());
            int editid=Integer.parseInt(Editid.getText());
            String edittype=Edittype.getText();
            float editmoney=Float.parseFloat(Editmoney.getText());
            String editdescriptions=Editdescription.getText();
            FamilyBookItem familyBookItem=new FamilyBookItem(editid,edittype,editmoney,editdate1,editdescriptions);
            String sql="insert into bill(taketype,money,days,descriptions) " +
                    "values('"+familyBookItem.getType()+"',"+familyBookItem.getMoney()+",'"+editdate1+"','"+familyBookItem.getDescription()+"')";
            smt=conn.createStatement();
            if(smt.execute(sql)==true) {
                JOptionPane.showMessageDialog(null,"添加失败");

            }else{

                JOptionPane.showMessageDialog(null,"添加成功");

            }
        }
        public void deleteItem() throws SQLException,ClassNotFoundException{
            Statement smt=null;
            ResultSet rs=null;
            String url="jdbc:mysql://localhost:3306/familybook?useSSL=false";
            Connection conn=JDBCUtils.getConnection(url);
            int keyitem=Integer.parseInt(Editid.getText());
            String sql="delete from bill where id='"+keyitem+"'";
            smt=conn.createStatement();
            if(smt.execute(sql)){
                JOptionPane.showMessageDialog(null,"删除失败");
            }else{
                JOptionPane.showMessageDialog(null,"删除成功");
            }
        }

    }



